<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <title>物品列表</title>
</head>
<script language="javascript">
    function GetType(type) {
        switch (type) {
            case 1: return "食品/药品";
            case 2: return "日用消耗品";
            case 3: return "工具/非消耗品";
            default: return "其他物品";
        }
    }
    function GetUnit(unit) {
        switch (unit) {
            case 1: return "个";
            case 2: return "台";
            case 3: return "箱";
            case 4: return "袋";
            case 5: return "瓶";
            case 6: return "盒";
            case 7: return "件";
            default: return "其他";
        }
    }
    function ConfirmDel() {
        if (confirm("真的要删除吗？")) {
            return true;
        }
        return false;
    }
    function LoadFunc(place, select) {
        if(select.note=="#隐藏#"){
            document.getElementById("select_id").style.display="none"
        }
        AddPlaceValue(place);
        SetSelected(select);
    }
    function SetSelected(select) {
        document.getElementById("name_id").value = select.name;
        var v_valid = document.getElementById("valid_id");
        for (j = 0; j < v_valid.length; j++) {
            if (v_valid.options[j].value == select.valid) {
                v_valid.options[j].selected = 'selected';
            }
        }
        var v_place = document.getElementById("place_id");
        for (j = 0; j < v_place.length; j++) {
            if (v_place.options[j].value == select.place) {
                v_place.options[j].selected = 'selected';
            }
        }
        var v_type = document.getElementById("type_id");
        for (j = 0; j < v_type.length; j++) {
            if (v_type.options[j].value == select.type) {
                v_type.options[j].selected = 'selected';
            }
        }
        document.getElementById("note_id").value = select.note;
    }
    function AddPlaceValue(places) {
        var pcs = document.getElementById("place_id");
        pcs.options.length = 0;
        pcs.options.add(new Option("所有位置"));
        for (var i = 0; i < places.length; i++) {
            pcs.options.add(new Option(places[i]));
        }
        return true;
    }
</script>
{% if messages %}
{% for msg in messages %}
<script>
    alert('{{ msg.message }}');
</script>
{% endfor %}
{% endif %}

<body onload="LoadFunc({{pls}},{{slt}})">
    <fieldset style="padding: 2">
        <legend>快捷链接</legend>
        <p><a href="add_object">录入物品</a> <a href="all_objects">所有物品</a> <a href="warning_objects">当前告警物品</a></p>
    </fieldset>
    <fieldset style="padding: 2" id="select_id">
        <legend>筛选条件</legend>
        <form method="POST" action="">
            <p>名称包含<input type="text" name="name" id="name_id">
                位置
                <select size="1" name="place" id="place_id"></select>
                类型<select size="1" name="type" id="type_id">
                    <option value="0" selected>所有类型</option>
                    <option value="1">食品/药品</option>
                    <option value="2">日用消耗品</option>
                    <option value="3">工具/非消耗品</option>
                    <option value="4">其他物品</option>
                </select>
                过期时间<select size="1" name="valid" id="valid_id">
                    <option value="0" selected>不限</option>
                    <option value="1">已过期</option>
                    <option value="2">一周内过期</option>
                    <option value="3">一个月内过期</option>
                    <option value="4">大于一个月过期</option>
                    <option value="5">不会过期</option>
                </select>
                备注包含<input type="text" name="note" id="note_id">
            </p>
            <p><input type="submit" value="查询" name="B1"> <input type="reset" value="重置" name="B2"></p>
        </form>

    </fieldset>
    <table border="1">
        <tr>
            <th>名称</th>
            <th>位置</th>
            <th>类型</th>
            <th>数量</th>
            <th>过期时间</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        {% for item in li %}
        <tr>
            <td>{{ item.name }}</td>
            <td>{{ item.place }}</td>
            <td>
                <script language="javascript">
                    document.write(GetType({{ item.type }}));
                </script>
            </td>
            <td>
                {{item.count}}
                <script language="javascript">
                    document.write(GetUnit({{ item.unit }}));
                </script>
            </td>
            <td>{{item.valid_date|date:"Y-n-j" }}</td>
            <td>{{item.note}}</td>
            <td>
                <a href="/edit_object?nid={{ item.id }}">编辑</a>|
                <a href="/delete_object?nid={{ item.id }}" onclick="javascript:return ConfirmDel();">删除</a>
            </td>
        </tr>
        {% endfor %}
</body>

</html>